<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
    mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
    props: {
        // 使用者window实例 必定义
        appWindow: {
            type: Object,
            default: null,
        },
    },
    components: {
        SelectImageCheckbox: async () =>
            Vue.extend(
                await Vue.prototype.$vFN.Components.Base.SelectImageCheckbox()
            ),
        SelectVideoRadio: async () =>
            Vue.extend(
                await Vue.prototype.$vFN.Components.Base.SelectVideoRadio()
            ),
    },
    data() {
        return {
            // 初始化状态
            initLoad: false,
            // 编辑类型
            editType: "",
            // 商品分类数据源
            categoryOptions: [],

            form: {
                // 商品类型  编辑时不可修改
                productType: this.$vFN.Constants.DictGoodsType.default,
                // 商品类目  编辑时不可修改
                categoryIds: [],
                // 商品名称
                name: "",
                // 商品标语
                subTitle: "",
                // 简称
                shortName: "",
                // SPU编码
                productCode: "",
                // 打开素材库，可多选，可排序，选中以后默认第一张为主图
                imageUrlLst: [],
                // 主图视频
                videoUrl: "",
                videoAlbumUrl: "",
            },
        };
    },

    methods: {
        validateLimit(rule, value, callback) {
            if (value !== "") {
                if (!this.$vFN.Utils.isNumberAndLetter(value)) {
                    callback(new Error("限数字和字母"));
                } else {
                    callback();
                }
            } else {
                callback();
            }
        },
        setVideoValue(res) {
            console.error(res);
            if (res.length > 0) {
                this.form.videoUrl = res[0].video;
                this.form.videoAlbumUrl = res[0].cover;
            }
        },
        validate(e) {
            return this.mixinEdit_formValidate("editForm");
        },
        getData() {
            let _d = {
                productType: this.form.productType,
                categoryId: "",
                subCategoryId: "",
                name: this.form.name,
                subTitle: this.form.subTitle,
                shortName: this.form.shortName,
                productCode: this.form.productCode,
                imageUrlLst: this.form.imageUrlLst,
                videoUrl: this.form.videoUrl,
                videoAlbumUrl: this.form.videoAlbumUrl,
            };
            if (this.form.categoryIds.length > 0) {
                _d.categoryId = this.form.categoryIds[0];
                _d.subCategoryId = this.form.categoryIds[1];
            }
            return _d;
        },
        async install(params_) {
            console.error("install", params_);
            this.editType = params_.type;

            // 数据源 商品类目
            this.categoryOptions = await this.$vFN.Tools.getSelectListSource(
                {
                    url: "mall-site-portal/product/listCategory",
                },
                this.appWindow
            );

            if (params_.datasource) {
                this.form.productType = params_.datasource.productType;
                this.form.categoryIds = [
                    params_.datasource.categoryId + "",
                    params_.datasource.subCategoryId + "",
                ];
                this.form.name = params_.datasource.name;
                this.form.subTitle = params_.datasource.subTitle;
                this.form.shortName = params_.datasource.shortName;
                this.form.productCode = params_.datasource.productCode;
                this.form.imageUrlLst = params_.datasource.imageUrlLst;
                this.form.videoAlbumUrl = params_.datasource.videoAlbumUrl;
                this.form.videoUrl = params_.datasource.videoUrl;
            }
            this.initLoad = true;
            this.returnData();
        },
        async uninstall() {
            this.$refs["editForm"].resetFields();
        },

        returnData() {
            this.$emit("change", {
                data: this.getData(),
                load: this.initLoad,
            });
        },
    },
    mounted() {},
    beforeCreate() {},
    watch: {
        form: {
            handler(newName, oldName) {
                this.returnData();
            },
            immediate: true,
            deep: true,
        },
    },
};
</script>
